package cn.wikiflyer.beikang.act.user.setting;

import java.lang.reflect.Type;

import net.tsz.afinal.annotation.view.ViewInject;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.act.cart.AddressData;
import cn.wikiflyer.beikang.act.user.setting.bean.WKVersionBean;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wk.libs4a.WKApplication;
import cn.wk.libs4a.net.WKAsyncTaskPro;
import cn.wk.libs4a.utils.WKCheckAppUpdate;
import cn.wk.libs4a.utils.WKCheckAppUpdate.CheckAppUpdateBack;
import cn.wk.libs4a.utils.WKCheckAppUpdate.GetDataForUpdate;
import cn.wk.libs4a.utils.WKUtils;
import cn.wk.libs4a.view.WKWebViewAct;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.umeng.message.PushAgent;

/**
 * 个人中心——设置
 * 
 * @author qunlee_mr
 * 
 */

public class UserSettingAct extends BaseActivity implements CheckAppUpdateBack {

	@ViewInject(id = R.id.tuisong_btn, click = "onClick")
	private Button tuison_btn;
	@ViewInject(id = R.id.oauth_btn, click = "onClick")
	private Button oauth_btn;
	@ViewInject(id = R.id.setting_feed, click = "onClick")
	private Button feedbackbtn;
	@ViewInject(id = R.id.setting_about, click = "onClick")
	private Button aboutbtn;
	@ViewInject(id = R.id.setting_check, click = "onClick")
	private Button versioncheckbtn;
	@ViewInject(id = R.id.user_setting_logout, click = "onClick")
	private Button user_setting_logout;
	@ViewInject(id = R.id.view_check_version, click = "onClick")
	private LinearLayout view_check_version;
	@ViewInject(id = R.id.version_txt)
	private TextView version_txt;
	//修改昵称、密码、手机号
	@ViewInject(id = R.id.ll_modify)
	private LinearLayout ll_modify;
	 @ViewInject(id = R.id.setting_modify_name, click = "onClick")
	 private Button btn_modify_name;
	 @ViewInject(id = R.id.setting_modify_pass, click = "onClick")
	 private Button btn_modify_pass;
	 @ViewInject(id = R.id.setting_modify_phone, click = "onClick")
	 private Button btn_modify_phone;

	private boolean ifTuisong = false;

	private PushAgent mPushAgent;

	@Override
	public void loadXml() {
		setContentView(R.layout.bk_act_user_setting);
	}

	@Override
	public void init() {
		mPushAgent = PushAgent.getInstance(ctx);
		if (mPushAgent.isEnabled()) {
			tuison_btn.setBackgroundResource(R.drawable.user_setting_checked);
		} else {
			tuison_btn.setBackgroundResource(R.drawable.user_setting_unchecked);
		}
		setShowStatus();
		app().settingAct = this;
	}

	@Override
	public void setData() {
		String versionName = WKUtils.getVersionName(ctx);
		version_txt.setText("当前版本号 " + versionName);
	}

	@Override
	protected void onResume() {
		super.onResume();
		setShowStatus();
	}
	/** 设置显示的状态
	 */
	private void setShowStatus(){
		if (app().getLoginSession() != null) {
			user_setting_logout.setText("注销当前帐号");
			ll_modify.setVisibility(View.VISIBLE);
		} else {
			user_setting_logout.setText("登录");
			ll_modify.setVisibility(View.GONE);
		}
	}

	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.tuisong_btn:
			if (mPushAgent.isEnabled()) {
				mPushAgent.disable();
				tuison_btn
						.setBackgroundResource(R.drawable.user_setting_unchecked);
				app().sp.setBoolean("push", false);
			} else {
				mPushAgent.enable();
				tuison_btn
						.setBackgroundResource(R.drawable.user_setting_checked);
				app().sp.setBoolean("push", true);
			}
			Log.e("wj", "isEnable : " + mPushAgent.isEnabled());
			break;
		case R.id.setting_feed:
			startActivity(new Intent(ctx, UserSettingFeedAct.class));
			break;
		case R.id.setting_about:
			startActivity(new Intent(ctx, UserSettingAboutAct.class));
			break;
		case R.id.oauth_btn:
			startActivity(new Intent(ctx, UserSettingOAuthAct.class));
			break;
		case R.id.user_setting_logout:
			if (app().getLoginSession() == null) {
				startActivity(new Intent(ctx, UserSettingLoginAct.class));
			} else {
				WKApplication.showMsg("是否注销当前登录?", "是", new OnClickListener() {
					public void onClick(DialogInterface arg0, int arg1) {
						new GetLoginoutTask(ctx);
					}
				}, "否", null);
			}
			break;
		case R.id.setting_check:
		case R.id.view_check_version:
			WKCheckAppUpdate.getInstance().setCheckUpdateback(this);
			// WKCheckAppUpdate.getInstance().clickCheck(this);
			WKCheckAppUpdate.getInstance().clickCheck(UserSettingAct.this,
					new GetDataForUpdate() {
						public String getData() {
							return app().net.get_version();
						}
					});
			break;
//		case R.id.setting_weibo:
//			Intent i = new Intent(ctx, WKWebViewAct.class);
//			i.putExtra("isFrom", "weibo");
//			i.putExtra("url", "http://www.qhbeikan.com/about/");
//			startActivity(i);
//			break;
		case R.id.setting_modify_name:			//修改昵称
			Intent modifyNameIntent = new Intent(ctx, UserSettingModifyNameAct.class);
			startActivity(modifyNameIntent);
			break;
		case R.id.setting_modify_pass:			//修改密码
			Intent modifyIntent = new Intent(ctx, UserSettingModifyPswAct.class);
			startActivity(modifyIntent);
			break;
		case R.id.setting_modify_phone:			//修改手机号
			Intent modifyPhoneIntent = new Intent(ctx, UserSettingModifyPhoneAct.class);
			startActivity(modifyPhoneIntent);
			break;
		default:
			break;
		}
	}

	/**
	 * 登出接口任务
	 */
	class GetLoginoutTask extends WKAsyncTaskPro {
		public GetLoginoutTask(Context context) {
			super(context);
		}

		@Override
		protected String doInBackground(boolean... params) {
			app().setLoginSession("");
			app().sp.setString("set-cookie", "");
			AddressData.getInstance(ctx).setListData(null);
			return app().net.loginOut();
		}

		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			// app().setLoginSession("");
			app().sp.setString("set-cookie", "");
//			user_setting_logout.setText("登录");
			setShowStatus();
		}
	}

	// @Override
	// public void checkBack(int result) {
	// if (result == 0) {
	// version_txt.setText(WKUtils.getVersionName(ctx) + " (已是最新版本)");
	// } else if (result > 0) {
	// version_txt.setText("" + WKUtils.getVersionName(ctx) + " (有新版可更新)");
	// }
	// }

	@Override
	public void checkBack(String result) {
		Log.e("wj", "result:" + result);
		Type cvbType = new TypeToken<BKBaseBean<WKVersionBean>>() {
		}.getType();
		Gson gson = new Gson();
		BKBaseBean<WKVersionBean> base = gson.fromJson(result, cvbType);
		if (base.status == 1) {
			int curVer = WKUtils.getVersionCode(activity);
			if (base.data.ver_num > curVer) {
				showDownloadDialog(base.data.url, base.data.ver_desc);
			} else {
				AlertDialog.Builder builder = new Builder(activity);
				builder.setTitle("更新提示");
				builder.setMessage("已经是最新版本了! ");
				builder.show();
			}
		} else {
			Toast.makeText(ctx, "访问服务器出错", Toast.LENGTH_SHORT).show();
		}
	}

	/**
	 * 弹出提示下载对话框
	 */
	public void showDownloadDialog(final String downloadUrl, String msg) {
		AlertDialog.Builder builder = new Builder(activity);
		builder.setTitle("提示");
		builder.setMessage(Html.fromHtml(msg));
		builder.setPositiveButton("确定",
				new android.content.DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						Uri uri = Uri.parse(downloadUrl);
						Intent intent = new Intent(Intent.ACTION_VIEW, uri);
						activity.startActivity(intent);
						dialog.dismiss();
					}
				});
		builder.setNeutralButton("取消",
				new android.content.DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		builder.create().show();
	}
}
